package org.igeek.atomqq.adapter;


import org.igeek.atomqq.AtomQQApplication;
import org.igeek.atomqq.activity.BaseActivity;
import org.igeek.atomqq.vo.Chat;
import org.igeek.atomqq.vo.MsgFriend;
import org.igeek.atomqq.widget.FriendMessageItemView;
import org.igeek.atomqq.widget.SendMessageItemView;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/** 
 * 会话记录适配器 
 * @author <a href="http://hangxin1940.cnblogs.com">hangxin1940@gmail.com</a>
 * @time 2012-2-2 下午05:27:30 
 */
public class MessageFriendListAdapter extends BaseAdapter{

	private AtomQQApplication application;
	private BaseActivity context;
	
	private long uin;
	
	public MessageFriendListAdapter(BaseActivity context,long uin) {
		application=(AtomQQApplication) context.getApplication();
		this.context=context;
		
		this.uin=uin;
	}
	

	@Override
	public int getCount() {
		return application.getChat(uin).size();
	}



	@Override
	public Object getItem(int position) {
		return application.getChat(uin);
	}



	@Override
	public long getItemId(int position) {
		Chat chat=application.getChat(uin).get(0);
		
		int type=chat.getType();
		
		if(type==Chat.FRIEND)
			return chat.getUin();
		else if(type==Chat.GROUP)
			return chat.getGcode();
		
		return 0;
	}



	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		MsgFriend gmessage=(MsgFriend) application.getChat(uin).get(position).getMessage();
		
		if(gmessage.getFrom_uin()==0){
			SendMessageItemView sview=new SendMessageItemView(context);
			sview.setMessage(gmessage);
			return sview;
		}
		
		FriendMessageViewHolder holder=new FriendMessageViewHolder(context);
		holder.chatView.setMessage(gmessage);
		return holder.chatView;
		
		
	}
	
	
	
	static class FriendMessageViewHolder{
		static FriendMessageItemView chatView;
		public FriendMessageViewHolder(BaseActivity context) {
			chatView=new FriendMessageItemView(context);
		}
	}



}
